<template>
    <div>
        <el-row>
            <el-col :span="24">
                <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
            </el-col>
        </el-row>

        <el-dialog title="新增banner" :visible.sync="dialogFormVisible">
            <!-- 新增 -->
            <template>
                <el-form ref="bannerAdd" status-icon :model="banner" key="bannerAdd">
                    <!-- <el-form-item  label="图片" prop="img" :label-width="formLabelWidth" :rules="[{required:true,pattern:/^.+(\.jpg|\.png|\.gif)(\?.+)?$/,message:'请传入正确格式的图片',trigger:'change'}]">
                                <el-input v-model="banner.img" autocomplete="off"></el-input>
                            </el-form-item> -->

                    <el-form-item label="本地图片" prop="img" :label-width="formLabelWidth">

                        <el-upload class="upload-demo" ref="upload" action=""
                            :on-change="changeHanler" :auto-upload="false">
                            <i class="el-icon-plus avatar-uploader-icon"></i>
                            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="提示" prop="alt" :label-width="formLabelWidth">
                        <el-input v-model="banner.alt" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="链接" prop="link" :label-width="formLabelWidth">
                        <el-input v-model="banner.link" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item style="text-align:center">
                        <el-button type="primary" @click="insertBannerImage">确 定</el-button>
                        <el-button @click="cancelHandler">取 消</el-button>
                    </el-form-item>
                </el-form>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { fileToBase64Async } from "@/utils/fileToBase64";
import { insertBannerListAPI } from "@/api/banner"
import _ from "lodash"

export default {
    name: "BannerAdd",
    data() {
        return {
            banner: {
                img: "",
                alt: "",
                link: ""
            },
            formLabelWidth: "100px",
            dialogFormVisible: false,
        }
    },
    methods: {
        insertBannerImage: _.debounce(async function () {
            if (this.banner.img) {
                try {
                    await insertBannerListAPI(this.banner)
                    this.$message.success("新增成功");
                } catch (err) {
                    this.$message.error("文件上传失败");
                }
            } else {
                this.$message.error("请先上传图片");
            }
        }),
        cancelHandler() {
            this.$refs.bannerAdd.resetFields()
        },
        async changeHanler(file, fileList) {
            console.log(file, fileList);  // file是组件处理之后的数据 -> 查找原始File数据(file.raw)
            try {
                var res = await fileToBase64Async(file.raw);
                console.log(res);
                this.banner.img = res;
            } catch (err) {
                console.log(err);
            }
        },
    }
}
</script>

<style lang="scss" scoped>
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    border-radius: 4px;
}
</style>